package com.smartcruise.util.network.customerObserver;

import com.smartcruise.base.RefreshContract;
import com.smartcruise.util.network.callBack.BaseCallBack;
import com.smartcruise.util.network.netUtils.NetContract;
import com.smartcruise.util.network.netUtils.Result;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by FengChaoQun
 * on 2018/3/18 0018
 * 和刷新组件配合的observer
 */

public class SimpleRefreshObserver implements Observer<Result> {

    protected Disposable disposable;
    private RefreshContract.View baseRefreshView;
    private BaseCallBack callBack;

    public SimpleRefreshObserver(RefreshContract.View baseRefreshView, BaseCallBack callBack) {
        this.baseRefreshView = baseRefreshView;
        this.callBack = callBack;
    }

    @Override
    public void onSubscribe(Disposable disposable) {
        this.disposable = disposable;
    }

    @Override
    public void onNext(Result result) {
        if (NetContract.isSuccess(result)) {
            callBack.onSuccess(result);
        } else {
            callBack.onFail(result);
        }
    }

    @Override
    public void onError(Throwable throwable) {
        baseRefreshView.getRefresh().finishLoadMore(false);
        baseRefreshView.getRefresh().finishRefresh(false);
        callBack.onError(throwable);
    }

    @Override
    public void onComplete() {
        baseRefreshView.getRefresh().finishLoadMore(true);
        baseRefreshView.getRefresh().finishRefresh(true);
    }

}
